package com.sos.view;

import java.util.ArrayList;

import com.sos.R;
import com.sos.activity.AddByContactorActivity;
import com.sos.adapter.SysContactorAdapter;
import com.sos.service.SysContactorManager;
import com.sos.vo.Contactor;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/***
 * 通讯录列表自定义view
 * 
 * **/
public class SysContactorListView extends ListView implements OnItemClickListener{
	private final String TAG = SysContactorListView.class.getSimpleName();
	
	private AddByContactorActivity parent;
	private ArrayList<Contactor> list = new ArrayList<Contactor>();
	private SysContactorAdapter adapter;
	private SysContactorManager sysContactorMgr;
	
	public SysContactorListView(Context context, AttributeSet attrs) {		
		super(context, attrs);
		setOnItemClickListener(this);
		Log.i(TAG, "SysContactorListView");
		
		sysContactorMgr = new SysContactorManager(getContext());
		adapter = new SysContactorAdapter(context, R.layout.sys_contactor_list_item, list);
		setAdapter(adapter);
		
		refresh();
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
		Log.i(TAG, "onItemClick");
		
		Contactor bean = (Contactor)adapter.getItemAtPosition(position);
		if(bean!=null){
			parent.doSelect(bean);
		}
	}
	
	public void setParent(AddByContactorActivity parent){
		this.parent = parent;
	}
	
	private void refresh(){
		Log.i(TAG, "refresh");
		
		list.clear();
		ArrayList<Contactor> data = sysContactorMgr.getList();
		for(Contactor bean : data){
			list.add(bean);
		}
		adapter.notifyDataSetChanged();
	}
}
